package com.qf.sl.youshare.tasks;

import android.os.AsyncTask;
import com.qf.sl.youshare.Constants;
import com.qf.sl.youshare.utils.HttpTools;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * Created by IDEA
 * User : sl
 * on 2015/2/5.
 * 17:02
 */
public class CategoryTask extends AsyncTask<String, Integer, JSONObject> {

    /**
     * 用于调用实际接收数据的对象,
     * 通常可以认为是 Activity/Fragment
     * 因为要进行UI的更新
     */
    private TaskProcessor processor;

    public CategoryTask(TaskProcessor processor) {
        this.processor = processor;
    }

    @Override
    protected JSONObject doInBackground(String... params) {

        JSONObject ret = null;

        byte[] bytes = HttpTools.get(Constants.ITEM_CATS_URL);

        if (bytes != null) {
            try {
                String str = new String(bytes,"utf-8");
                ret = new JSONObject(str);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {
        if (processor != null) {
            processor.processResult(jsonObject);
        }
    }
}
